//
//  giftInfoDatabase.m
//  tcpoctest
//
//  Created by gump on 5/27/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "giftInfoDatabase.h"

#import "GiftInfoDataSource.h"

#import "GiftInfoDataInfo.h"

@implementation giftInfoDatabase

-(void)creatTable
{
    if (db == nil) {
        return;
    }
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    [db executeUpdate:@"create table if not exists giftinfo(giftid text,\
giftprice int,giftstate char(1),giftname text,gifturl text,giftbelongid char(1),\
giftismusic char(1),giftmusicurl text)"];
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)initData
{
    if (db == nil) {
        return;
    }
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    NSString *strsql = [NSString stringWithFormat:@"SELECT * FROM giftinfo"];
    FMResultSet *s = [db executeQuery:strsql];
    
    [[DataControl shareControl].giftInfoDS delAllGIDic];
    
    while ([s next])
    {
        GiftInfoDataInfo    *ginfo = [[GiftInfoDataInfo alloc] init];
        ginfo.strgiftid = [s stringForColumn:@"giftid"];
        ginfo.strgiftname = [s stringForColumn:@"giftname"];
        ginfo.strgifturl = [s stringForColumn:@"gifturl"];
        ginfo.giftprice = [s intForColumn:@"giftprice"];
        ginfo.giftstate = [s intForColumn:@"giftstate"];
        ginfo.giftBelongID = [s intForColumn:@"giftbelongid"];
        ginfo.giftIsMusic = [s intForColumn:@"giftismusic"];
        ginfo.giftMusicUrl = [s stringForColumn:@"giftmusicurl"];
        
        [[DataControl shareControl].giftInfoDS AddGIDicInfo:ginfo withGiftID:ginfo.strgiftid];
    }
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)insertInfo:(GiftInfoDataInfo *)ginfo
{
    if (db == nil) {
        return;
    }
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    NSString *strsql = [NSString stringWithFormat:@"SELECT * FROM giftinfo WHERE giftid = '%@'",
                        ginfo.strgiftid];
    
    
    FMResultSet *s = [db executeQuery:strsql];
    if([s next])
    {
        strsql = [NSString stringWithFormat:@"update giftinfo \
set giftprice = %d,giftstate = '%d',giftname = '%@',\
gifturl = '%@',giftbelongid = '%d',giftismusic = '%d',\
giftmusicurl = '%@' WHERE giftid = '%@'"
                  ,ginfo.giftprice
                  ,ginfo.giftstate
                  ,ginfo.strgiftname
                  ,ginfo.strgifturl
                  ,ginfo.giftBelongID
                  ,ginfo.giftIsMusic
                  ,ginfo.giftMusicUrl
                  ,ginfo.strgiftid];
    }
    else
    {
        strsql = [NSString stringWithFormat:@"insert into giftinfo \
(giftprice,giftid,giftstate,giftname,gifturl,giftbelongid,giftismusic,giftmusicurl) \
values(%d,'%@','%d','%@','%@','%d','%d','%@')"
                ,ginfo.giftprice
                ,ginfo.strgiftid
                ,ginfo.giftstate
                ,ginfo.strgiftname
                  ,ginfo.strgifturl
                  ,ginfo.giftBelongID
                  ,ginfo.giftIsMusic
                  ,ginfo.giftMusicUrl];
    }
    
    [db executeUpdate:strsql];
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

@end
